package cn.king.org.controller;

import cn.king.basic.annotation.PreAuthorize;
import cn.king.basic.util.AjaxResult;
import cn.king.basic.util.PageList;
import cn.king.org.domain.Department;
import cn.king.org.query.DepartmentQuery;
import cn.king.org.service.DepartmentServiceI;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 接口类：部门接口
 */
//@ResponseBody
//@Controller
@Api(value = "部门接口类",description = "部门相关的CRUD功能") /** 在swagger页面就有该注释 */
@RestController /** 底层有@ResponseBody+@Controller */
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    private DepartmentServiceI departmentService;

    /** 接口：查询单个数据 */
    @ApiOperation(value = "接口：查询单个数据") /** 在swagger页面就有该注释 */
    @GetMapping("/{id}") /** 前端：get http://localhost:8080/department/1 */
    @PreAuthorize(name = "查询单个部门",sn = "department:getOne")
    public Department findOne(@PathVariable("id")Long id){
        return departmentService.getOne(id);
    }

    /** 接口：查询所有数据 */
    @ApiOperation(value = "接口：查询所有数据") /** 在swagger页面就有该注释 */
    @GetMapping /** 前端：get http://localhost:8080/department */
    public List<Department> findAll(){
        return departmentService.getAll();
    }

    /** 接口：删除数据 */
    @ApiOperation(value = "接口：删除数据") /** 在swagger页面就有该注释 */
    @DeleteMapping("{id}") /** 前端：delete http://localhost:8080/department/1 */
    public AjaxResult delete(@PathVariable("id")Long id){
        try {
            departmentService.delete(id);
            //删除成功
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /** 接口：批量删除数据 */
    @ApiOperation(value = "接口：删除数据") /** 在swagger页面就有该注释 */
    @PatchMapping /** 前端：delete http://localhost:8080/department/[1,2,3] */
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
            departmentService.patchDelete(ids);
            //删除成功
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /** 接口：添加或修改数据 */
    @ApiOperation(value = "接口：添加或修改数据") /** 在swagger页面就有该注释 */
    @PutMapping /** 前端：put http://localhost:8080/department json对象{} */
    public AjaxResult addAndUpdate(@RequestBody Department department){
        try {
            if (department.getId() == null){ //当ID为空
                //调用添加方法
                departmentService.add(department);
            }else { //ID不为空
                //执行修改方法
                departmentService.update(department);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /** 接口：分页和高级查询 */
    @ApiOperation(value = "接口：分页和高级查询") /** 在swagger页面就有该注释 */
    @PostMapping /** 前端：post http://localhost:8080/department json对象{} */
    public PageList<Department> queryPage(@RequestBody DepartmentQuery departmentQuery){
        return departmentService.queryPage(departmentQuery);
    }

    /** 接口：部门树 */
    @ApiOperation(value = "接口：查询部门树") /** 在swagger页面就有该注释 */
    @GetMapping("/deptTree")
    public List<Department> deptTree(){
        return departmentService.deptTree();
    }
}
